<?php

namespace common\response;

use  Yii;
use yii\helpers\Url;

class Response
{
    function susess($msg = '', $data = [])
    {
        self::response(200,  $msg, $data, md5(time()), true);
    }

    function error($msg = '', $data = [], $code=401, $errorData=[])
    {
        self::response($code, $msg, $data, md5($msg), false, $errorData);
    }

    function paging($data, $msg = '')
    {
        $data['code'] = !count($data['data'])?-1:(empty($msg) ? 0 : -1);
        $data['msg'] = !count($data['data'])?'暂无相关数据':$msg;
        \Cm::$app->log->access(true, $data);
        echo json_encode($data);
        exit;
    }

    function render($context, $view, $data = [])
    {
        \Cm::$app->log->access(true, $data);
        return $context->render($view, $data);
    }

    function redirect($route,$param='')
    {
        $res = Yii::$app->getResponse()->redirect(Url::to($route).$param, 302);
        $res->send();
        exit;
        //$context->redirect($route)->send();
    }
    private static function response($code=401,  $msg, $data, $md5='', $status=false,  $errorData=[])
    {
        $res = ['code' => $code, 'flag' => $status, 'msg' => $msg, 'data' => $data, 'success'=> $status,'errorData'=> $errorData, 'md5'=>$md5, 'alg'=> 'SALT_MD5'];
        \Cm::$app->log->access(true, $res);
        echo json_encode($res);        
        exit;
    }
}